home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / wbargs.c < prev   
C/C++ Source or Header  |  1998-09-30  |  5KB  |  209 lines

  1. /* Copyright (c) 1993 SAS Institute, Inc, Cary, NC USA */
  2. /* All Rights Reserved */
  3.  
  4. /* Adapted for Python1.4 by Irmen de Jong, 28 dec. 1996 */
  5.  
  6. /* Changed the order in which the Workbench Tooltypes are
  7.    passed to the interpreter. For Python 1.5.1 build 13. */
  8.  
  9. #include <workbench/startup.h>
  10. #include <exec/execbase.h>
  11. #include <string.h>
  12. #include <dos.h>
  13. #include <stdlib.h>
  14. #include <constructor.h>
  15.  
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <proto/icon.h>
  19.  
  20. /* These two symbols, _WBArgc and _WBArgv, are initialized if     */
  21. /* the program was invoked from WorkBench.  They look like normal */
  22. /* C (argc, argv) parameters.  The parameters are gathered as     */
  23. /* follows:                                                       */
  24. /*   Name of the program                                          */
  25. /*   Any tooltypes specified in the ToolTypes array               */
  26. /*   Any icons supplied as arguments (with SHIFT-CLICK)           */
  27.  
  28. int _WBArgc;       /* Count of the number of WorkBench arguments */
  29. char **_WBArgv;    /* The actual arguments                       */
  30.  
  31. static int _WBArgMax;  /* Internal: tells us how much space is left */
  32.                        /* in _WBArgv.                               */
  33.  
  34. // NAME:    FullName
  35. // PURPOSE: get the full pathname of a file.
  36. static void FullName(BPTR parent, char *name, char *buf, int len)
  37. {
  38.     int i = 0;
  39.     if(NameFromLock(parent, buf, len-1))
  40.     {
  41.         i = strlen(buf);
  42.         if(buf[i-1] != ':' && buf[i-1] != '/')
  43.         {
  44.             buf[i++] = '/';
  45.             buf[i] = 0;
  46.         }
  47.     }
  48.  
  49.     if(i < len-1) strncpy(buf+i, name, len-i-1);
  50.     return;
  51. }
  52.  
  53. /* Add an argument to the _WBArgv array.  We must allocate */
  54. /* memory for the argument and copy the incoming data.     */
  55. static int AddArg(char *arg)
  56. {
  57.     if(_WBArgc >= _WBArgMax-1)
  58.     {
  59.         /* Out of space in _WBArgv.  Reallocate it bigger. */
  60.         _WBArgMax += 10;
  61.         _WBArgv = realloc(_WBArgv, _WBArgMax*sizeof(char *));
  62.         if(_WBArgv == NULL) return -1;
  63.     }
  64.  
  65.     /* Allocate memory for the new argument */
  66.     _WBArgv[_WBArgc] = malloc(strlen(arg)+1);
  67.     if(_WBArgv[_WBArgc] == NULL) return -1;
  68.  
  69.     /* Copy the argument data over */
  70.     strcpy(_WBArgv[_WBArgc], arg);
  71.  
  72.     /* Increment our argument count */
  73.     _WBArgc++;
  74.  
  75.     return 0;
  76. }
  77.  
  78.  
  79. static void ReorderArgv(int argc, char** argv)
  80. {
  81.     int i;
  82.     for(i=1; i<argc; i++)
  83.     {
  84.         // check if it is the 'magic tooltype' PYTHONSCRIPT
  85.         if(strnicmp("PYTHONSCRIPT=",argv[i],13)==0)
  86.         {
  87.             short j;
  88.             char *thisarg;
  89.             // skip the magic word
  90.             strcpy(argv[i], argv[i]+13);
  91.             // place this tooltype as the first after the programname
  92.             thisarg = argv[i];
  93.             for(j=i; j>1; )
  94.             {
  95.                 argv[j]=argv[--j];
  96.             }
  97.             argv[1]=thisarg;
  98.         }
  99.  
  100.         // check if it is the 'magic tooltype' PYSCRIPTARG
  101.         if(strnicmp("PYSCRIPTARG=",argv[i],12)==0)
  102.         {
  103.             short j;
  104.             char *thisarg;
  105.             // skip the magic word
  106.             strcpy(argv[i], argv[i]+12);
  107.             // place this tooltype at the end of the array.
  108.             thisarg = argv[i];
  109.             for(j=i; j<argc; j++)
  110.             {
  111.                 argv[j]=argv[j+1];
  112.             }
  113.             argv[argc-1]=thisarg;
  114.             i--;
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. /* This is an autoinitializer routine that will be run if this  */
  122. /* module is linked in to user code.  Referring to the external */
  123. /* data items above is sufficient to pull this in from a library*/
  124.  
  125. // int __stdargs _STI_20000_WBArgParse(void)
  126. CONSTRUCTOR_P(WBArgParse,20000)
  127. {
  128.     struct WBArg *wba;
  129.     int nargs;
  130.     char buf[512];
  131.     struct DiskObject *dob;
  132.     struct Library *IconBase;
  133.     BPTR dir;
  134.  
  135.     if(_WBenchMsg == NULL) return 0;  // Not invoked from WorkBench
  136.  
  137.     /* Put the program name in */
  138.     wba=_WBenchMsg->sm_ArgList;
  139.     FullName(wba->wa_Lock, wba->wa_Name, buf, sizeof(buf));
  140.     if(AddArg(buf)) return -1;
  141.  
  142.     /* Find the tool types and add them */
  143.  
  144.     if(IconBase = OpenLibrary("icon.library", 0L))
  145.     {
  146.         dir = CurrentDir(wba->wa_Lock);
  147.         if(dob = GetDiskObject(wba->wa_Name))
  148.         {
  149.             if(dob->do_ToolTypes)
  150.             {
  151.                 for(nargs=0; dob->do_ToolTypes[nargs]; nargs++)
  152.                     if(AddArg(dob->do_ToolTypes[nargs])) return -1;
  153.             }
  154.             FreeDiskObject(dob);
  155.         }
  156.         CurrentDir(dir);
  157.         CloseLibrary(IconBase);
  158.     }
  159.  
  160.  
  161.     /* Now add the file arguments */
  162.     /* For Python1.4, also insert the tooltypes of the first file (if any) */
  163.     for(nargs=1, wba++; 
  164.         nargs<_WBenchMsg->sm_NumArgs;
  165.         nargs++, wba++)
  166.     {
  167.         // add only the tooltypes from the FIRST file (=the script)
  168.         if(nargs==1)
  169.         {
  170.             if(IconBase = OpenLibrary("icon.library", 0L))
  171.             {
  172.                 dir=CurrentDir(wba->wa_Lock);
  173.                 if(dob = GetDiskObject(wba->wa_Name))
  174.                 {
  175.                     if(dob->do_ToolTypes)
  176.                     {
  177.                         int i;
  178.                         for(i=0; dob->do_ToolTypes[i]; i++)
  179.                             if(AddArg(dob->do_ToolTypes[i])) return -1;
  180.                     }
  181.                     FreeDiskObject(dob);
  182.                 }
  183.                 CurrentDir(dir);
  184.                 CloseLibrary(IconBase);
  185.             }
  186.         }
  187.  
  188.         FullName(wba->wa_Lock, wba->wa_Name, buf, sizeof(buf));
  189.         {
  190.             BPTR test=Lock(buf,ACCESS_READ);
  191.             if(test)
  192.             {
  193.                 UnLock(test);
  194.                 if(AddArg(buf)) return -1;
  195.             }
  196.         }
  197.     }
  198.  
  199.     /* Make sure _WBArgv is terminated with a NULL pointer */
  200.     /* like ANSI C argv lists are.                         */
  201.     _WBArgv[_WBArgc] = NULL;
  202.  
  203.  
  204.     // reorder the options
  205.     ReorderArgv(_WBArgc,_WBArgv);
  206.  
  207.     return 0;
  208. }
  209.